﻿using e_commerce.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.BLL;
using e_commerce.BLL.Services.Enterprise;
using System.IO;
using System.Web.Helpers;
using e_commerce.BLL.Services;
using e_commerce.BLL.Data;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    public class AdminController : Controller
    {
        private readonly PermissionsService _pservice;

        public AdminController()
        {
            _pservice = new PermissionsService();
        }

        public ActionResult Home()
        {

            return View();
        }

        
        public ActionResult Menu()//获取菜单
        {
            var user = Session["ADMIN"] as AdminUser;
            var menus = _pservice.GetPermissionsMenu(user.UserId);
            IEnumerable<Models.Menu> viewMenu = menus.Select(m => new Models.Menu()
            {
                MenuId = m.MenuId,
                ParentId = m.ParentId,
                MenuBody = m.MenuName,
                Url = m.Url,
                Icon = m.Icon,
            });

            viewMenu = GetViewMenu(viewMenu.ToList());
            return PartialView("ViewComponents/Main_menu", viewMenu);
        }

        private List<Models.Menu> GetViewMenu(List<Models.Menu> list)
        {
            List<Models.Menu> vList = list.Where(l => l.ParentId == 0).ToList();
            foreach (var vl in vList)
            {
                GetSubMenu(vl, list);
            }
            return vList;
        }

        private void GetSubMenu(Models.Menu menu, List<Models.Menu> list)
        {
            if (list == null)
            {
                return;
            }
            var vList = list.Where(l => l.ParentId == menu.MenuId);
            if (vList.Count() != 0)
            {
                menu.Child.AddRange(vList);
                foreach (var c in menu.Child)
                {
                    GetSubMenu(c, list);
                }
            }
        }

      
    }
}